package cn.hx.admin.service.impl;

import cn.hx.admin.entity.Dept;
import cn.hx.admin.entity.Project;
import cn.hx.admin.dao.ProjectDao;
import cn.hx.admin.service.DeptService;
import cn.hx.admin.service.ProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Project)表服务实现类
 *
 * @author makejava
 * @since 2023-12-15 13:56:25
 */
@Service("projectService")
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private ProjectDao projectDao;

    @Resource
    private DeptService deptService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Project queryById(Integer id) {
        return this.projectDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param project 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Project> queryByPage(Project project) {
        return this.projectDao.queryAll(project);
    }

    /**
     * 新增数据
     *
     * @param project 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Project insert(Project project) {
        System.out.println(project);
        Project pro = new Project();
        pro.setDeptId(project.getDeptId());
        List<Project> projects = projectDao.queryAll(pro);
        for (Project project1 : projects) {
            projectDao.deleteById(project1.getId());
        }
        List<String> maps = project.getMap();
        pro.setName(project.getName());
        for (String map : maps) {
            String[] split = map.split(",");
            pro.setSort(Integer.valueOf(split[0]));
            pro.setLongitude(split[1]);
            pro.setLatitude(split[2]);
            projectDao.insert(pro);
        }
        Dept dept = new Dept();
        dept.setId(pro.getDeptId());
        dept.setLongitude(Double.valueOf(project.getLongitude()));
        dept.setLatitude(Double.valueOf(project.getLatitude()));
        System.out.println(dept);
        deptService.update(dept);
        return project;
    }

    /**
     * 修改数据
     *
     * @param project 实例对象
     * @return 实例对象
     */
    @Override
    public Project update(Project project) {
        this.projectDao.update(project);
        return this.queryById(project.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.projectDao.deleteById(id) > 0;
    }

}
